<template>
  <div class="detail-container">
    <el-button class="external-back-btn" type="success" round @click="goBack">返回</el-button>

    <!-- 商品详情卡片 -->
    <div class="detail-card">
      <el-container>
        <!-- 头部区域：商品标题和分类 -->
        <el-header class="detail-header">
          <div class="header-content">
            <h1 class="product-title" v-html="product.productName"></h1>
            <div class="header-right">
              <span class="inline-price">¥{{ product.price?.toFixed(2) }}</span>
            </div>
          </div>
          <div class="post-category">分类：{{ categoryName }}</div>
        </el-header>

        <!-- 主体区域：商品描述 -->
        <el-main class="detail-main">
          <article class="article-content">
            <div class="product-info">
              <div class="content-body description-content" v-html="product.description"></div>
            </div>
          </article>
        </el-main>

        <!-- 底部区域：商品价格（保留作为备用） -->
        <el-footer class="detail-footer" v-if="false">
          <div class="price-container">
            <span class="price-label">价格：</span>
            <span class="price-value">¥{{ product.price?.toFixed(2) }}</span>
          </div>
        </el-footer>
      </el-container>
    </div>
  </div>
</template>

<script>
import productApi from "@/api/product";
import categoryApi from "@/api/category";

export default {
  name: 'ProductDetail',
  data() {
    return {
      product: {}, // 商品详情数据
      categoryName: '',
    };
  },
  created() {
    this.loadProductDetail();
    this.load();
  },
  methods: {
    async load() {
      try {
        // 获取商品详情
        const productRes = await productApi.getById(this.$route.query.productId);
        this.product = productRes.data;

        // 获取分类名称
        if (this.product.categoryId) {
          const categoryRes = await categoryApi.getById(this.product.categoryId);
          this.categoryName = categoryRes.data.categoryName;
        }

        this.adjustDescriptionImages();
      } catch (error) {
        console.error("加载数据失败:", error);
      }
    },
    /**
     * 加载商品详情
     */
    loadProductDetail() {
      productApi.getById(this.$route.query.productId).then((res) => {
        this.product = res.data;
        this.adjustDescriptionImages();
      });
    },

    /**
     * 调整描述中的图片样式
     */
    adjustDescriptionImages() {
      this.product.description = this.product.description?.replace(
        /<img([^>]*)>/gi,
        (match, attributes) => {
          const cleanedAttributes = attributes.replace(/style="[^"]*"/i, "");
          return `<img${cleanedAttributes} style="max-width: 100%; height: auto; object-fit: contain; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin: 1rem 0;">`;
        }
      );
    },

    /**
     * 返回上一页
     */
    goBack() {
      try {
        this.$router.go(-1);
      } catch (error) {
        this.$router.push("/");
      }
    }
  }
};
</script>

<style scoped>
/* 新增外部返回按钮样式 */
.external-back-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 10;
  padding: 10px 20px !important;
  font-size: 1rem;
  font-weight: 500;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%) !important;
  border: none !important;
  box-shadow: 0 4px 15px rgba(79, 172, 254, 0.4) !important;
  transition: all 0.3s ease !important;
  color: white !important;
}

.external-back-btn:hover {
  transform: translateY(-3px) !important;
  box-shadow: 0 8px 20px rgba(79, 172, 254, 0.6) !important;
}

.external-back-btn:active {
  transform: translateY(1px) !important;
}

/* 调整容器定位 */
.detail-container {
  position: relative; /* 新增 */
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  background: linear-gradient(135deg, #e4e8eb 0%, #e4e8eb 100%);
}

/* 整体页面背景改为柔和渐变色 */
.detail-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  background: linear-gradient(35deg, #90f9d5 0%, #ffc265 100%);
}

/* 商品卡片容器 - 现代卡片设计 */
.detail-card {
  width: 90%;
  max-width: 1000px;
  margin: 0 auto;
  background: rgb(254, 250, 208);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.detail-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
}

/* 头部区域 - 现代渐变设计 */
.detail-header {
  min-height: 120px;
  padding: 1.5rem 2rem;
  background: linear-gradient(135deg, #c5e081 0%, #5f7bf8 100%);
  color: white;
  position: relative;
  overflow: hidden;
}

.detail-header::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  transform: rotate(30deg);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.8rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.product-title {
  font-size: 2rem;
  margin: 0;
  font-weight: 700;
  position: relative;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
  max-width: 70%;
  line-height: 1.3;
}

.post-category {
  font-size: 1rem;
  opacity: 0.9;
  background: rgba(255, 255, 255, 0.15);
  display: inline-block;
  padding: 0.3rem 1rem;
  border-radius: 20px;
  backdrop-filter: blur(5px);
}

/* 价格显示样式 */
.inline-price {
  font-size: 1.8rem;
  font-weight: 700;
  color: #ffd700;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}


/* 主体区域 - 优化阅读体验 */
.detail-main {
  padding: 2.5rem;
  line-height: 1.7;
  color: #333;
}

.article-content {
  max-width: 800px;
  margin: 0 auto;
}

.product-info {
  font-size: 1.05rem;
}

.description-content {
  word-break: break-word;
}

.description-content >>> p {
  margin-bottom: 1.5rem;
  color: #444;
}

.description-content >>> h2,
.description-content >>> h3 {
  margin-top: 2rem;
  margin-bottom: 1rem;
  color: #222;
}

.description-content >>> a {
  color: #667eea;
  text-decoration: none;
  transition: color 0.2s;
}

.description-content >>> a:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 响应式设计 */
@media screen and (max-width: 768px) {
  .detail-container {
    padding: 10px 0;
  }
  
  .detail-card {
    width: 95%;
  }
  
  .detail-header {
    padding: 1.2rem;
  }
  
  .product-title {
    font-size: 1.6rem;
    max-width: 60%;
  }
  
  .inline-price {
    font-size: 1.4rem;
  }
  
  .header-back-btn {
    padding: 0.5rem 1.2rem !important;
    font-size: 0.9rem;
  }
  
  .detail-main {
    padding: 1.5rem;
  }
  .external-back-btn {
    top: 15px;
    right: 15px;
    padding: 8px 16px !important;
    font-size: 0.9rem;
  }
}

@media screen and (max-width: 480px) {
  .header-content {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1rem;
  }
  
  .header-right {
    width: 100%;
    justify-content: space-between;
  }
  
  .product-title {
    font-size: 1.4rem;
    max-width: 100%;
  }
  
  .inline-price {
    font-size: 1.3rem;
  }
  
.external-back-btn {
    top: 10px;
    right: 10px;
    padding: 6px 12px !important;
    font-size: 0.8rem;
  }
  
  .detail-main {
    padding: 1rem;
  }
  
  .post-category {
    font-size: 0.9rem;
  }
}
</style>